package com.jjrwa.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import org.jetbrains.annotations.NotNull;

public abstract class JavaFileAction extends AnAction {

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT; // IDEA 2023.2+ 推荐设置
    }


    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        e.getPresentation().setEnabledAndVisible(false);
        PsiElement psiElement = e.getData(PlatformDataKeys.PSI_ELEMENT);//鼠标所在的元素
        if (psiElement == null) {
            return;
        }
        PsiFile containingFile = psiElement.getContainingFile();
        if (containingFile != null && (containingFile instanceof PsiJavaFile)) {
            e.getPresentation().setVisible(true);
            PsiJavaFile psiJavaFile = (PsiJavaFile) containingFile;
            PsiAnnotation ann = psiJavaFile.getClasses()[0].getAnnotation(judgeAnnotation());
            if (ann == null) {
                return;
            }
            e.getPresentation().setEnabledAndVisible(true);
        }
    }

    protected abstract String judgeAnnotation();
}
